﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BanditGame2.WCFService;

namespace BanditGame2
{
    public partial class SignUpWindow : ChildWindow
    {
        User user = new User();
        public SignUpWindow()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            tbError.Text = "";
            tbNameStar.Text = "";
            tbSurnameStar.Text = "";
            tbEmailStar.Text = "";
            tbLoginStar.Text = "";
            tbPasswordStar.Text = "";
            tbPassword2Star.Text = "";




            bool allFilled = true;
            if (tbxName.Text == "")
            {
                tbNameStar.Text = "*";
                allFilled = false;
            }
            if (tbxSurname.Text == "")
            {
                tbSurnameStar.Text = "*";
                allFilled = false;
            }
            if (tbxEmail.Text == "")
            {
                tbEmailStar.Text = "*";
                allFilled = false;
            }
            if (tbxLogin.Text == "")
            {
                tbLoginStar.Text = "*";
                allFilled = false;
            }
            if (tbxPassword.Password == "")
            {
                tbPasswordStar.Text = "*";
                allFilled = false;
            }
            if (tbxPassword2.Password == "")
            {
                tbPassword2Star.Text = "*";
                allFilled = false;
            }
            if (tbxPassword.Password != tbxPassword2.Password)
            {
                tbError.Text = "Hesla se neshodují.";
                tbxPassword.Password = "";
                tbxPassword2.Password = "";
                allFilled = false;
            }
            if (allFilled)
            {
                user.Jmeno = tbxName.Text;
                user.Prijmeni = tbxSurname.Text;
                user.Email = tbxEmail.Text;
                user.Login = tbxLogin.Text;
                user.Password = tbxPassword.Password;
                user.Admin = false;

                WCFService.WCFServiceClient WCFClient = new WCFService.WCFServiceClient();
                WCFClient.checkExistingUserCompleted += new EventHandler<checkExistingUserCompletedEventArgs>(checkIfUserExists);
                WCFClient.checkExistingUserAsync(user.Login);
                
            }



        }

        private void checkIfUserExists(object sender, checkExistingUserCompletedEventArgs e)
        {
            if (e.Result)
            {
                WCFService.WCFServiceClient WCFClient = new WCFService.WCFServiceClient();
                WCFClient.insertNewUserCompleted += new EventHandler<insertNewUserCompletedEventArgs>(insertNewUserCompleted);
                WCFClient.insertNewUserAsync(user);
            }
            else
            {
                tbError.Text = "tento login jiz existuje.";
            }
        }


        private void insertNewUserCompleted(object sender, insertNewUserCompletedEventArgs e)
        {
            if (e.Result)
            {
                this.DialogResult = true;
            }
            else
            {
                tbError.Text = "Něco se porouchalo!";
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

